package com.example.yyjc.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.yyjc.domain.countryanalyze.Country;
import com.example.yyjc.domain.countryanalyze.CountryToponeShopMsg;
import com.example.yyjc.domain.countryanalyze.LogisticsOrder;
import com.example.yyjc.domain.countryanalyze.MainTitleScale;
import com.example.yyjc.service.CountryService;
import com.example.yyjc.util.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName CountryController
 * @Description TODO      国家分析  (详情内容见美国国家市场分析报告)
 * @Author lvyongwei
 * @Date 2020-11-11 14:36
 **/
@RestController
@CrossOrigin
@RequestMapping("/country")
public class CountryController {

    @Autowired
    private CountryService countryService;

    Logger logger = LoggerFactory.getLogger(CountryController.class);

    /**
     * 获取所有时间
     *
     * @return
     */
    @PostMapping("/list")
    public ResultBean getList() {
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTime();
        /**
         * 前端展示内容
         */
        List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        for (String time : times) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("month", time);
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            jsonObject.put("monthFormat", sb.toString());
            jsonObjects.add(jsonObject);
        }
        return ResultBean.success(jsonObjects);
    }


    /**
     * 根据日期获取所有国家
     *
     * @param data {time"时间"}
     * @return
     */
    @PostMapping("/countryOrderCount")
    public ResultBean getAllCountry(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String time = jsonObject.getString("time");
        try {
            List<Country> countries = countryService.getCountryOrderCount(time);
            return ResultBean.success("获取数据成功", countries);
        } catch (Exception e) {
            return ResultBean.error("获取数据失败");
        }
    }

    /**
     * 获取前十国家数据
     *
     * @return
     */
    @PostMapping("/countryOrderCountByTime")
    public ResultBean getAllLimit(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String time1 = jsonObject.getString("time");
        /**
         * 定义返回对象
         */
        ResultBean resultBean = new ResultBean();
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTime();
        //获取开始时间和结束时间再集合的位置
        if (StringUtils.isEmpty(countryName) || countryName == null) {
            try {
                /**
                 * 获取前十国家名称
                 */
                List<String> countryNames = countryService.getCountryName(time1);
                /**
                 * 数据
                 */
                List<JSONObject> values = new ArrayList<JSONObject>();
                for (String countryName1 : countryNames) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("name", countryName1);
                    jsonObject1.put("data", countryService.getCountryOrderCountByTime(countryName1, times));
                    //List<Country> lists = countryService.getCountryOrderCountByTime(countryName1,times);
                    //values.add(lists);
                    values.add(jsonObject1);
                }
                resultBean.put("lines", countryNames);
                List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
                for (String time : times) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("month", time);
                    StringBuilder sb = new StringBuilder(time);
                    sb.insert(4, "年");
                    sb.append("月");
                    jsonObject1.put("monthFormat", sb.toString());
                    jsonObjects.add(jsonObject1);
                }
                resultBean.put("xnames", jsonObjects);
                resultBean.put("yvalues", values);
                resultBean.put("code", 200);
                return resultBean;
            } catch (Exception e) {
                return ResultBean.error("获取数据失败");
            }
        } else {
            try {
                List<Double> values = countryService.getCountryOrderCountByTime(countryName, times);
                resultBean.put("lines", countryName);
                List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
                for (String time : times) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("month", time);
                    StringBuilder sb = new StringBuilder(time);
                    sb.insert(4, "年");
                    sb.append("月");
                    jsonObject1.put("monthFormat", sb.toString());
                    jsonObjects.add(jsonObject1);
                }
                resultBean.put("xnames", jsonObjects);
                resultBean.put("yvalues", values);
                resultBean.put("code", 200);
                return resultBean;
            } catch (Exception e) {
                return ResultBean.error("获取数据失败");
            }
        }
    }


    /**
     * 获取国家各个月份订单量以及环比增幅  (国家分析 第 5-1,5-2)
     *
     * @param data
     * @return
     */
    @PostMapping("/getCountryCount")
    public ResultBean getCountryCount(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        List<String> time2 = countryService.getAllTimeAsc2();
        int begin = time2.indexOf(beginTime);
        int end = time2.indexOf(endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 获取国家订单量
         */
        List<Double> values = countryService.getCountryOrderCountByTime(countryName, time2);
        if (StringUtils.isEmpty(values) || values.size() == 0) {
            return ResultBean.error("抱歉，搜索不到该国家");
        }
        /**
         * 获取环比增幅
         */
        List<String> values2 = new ArrayList<String>();
        for (int i = 0; i < values.size(); i++) {
            if (i != 0) {
                values2.add(String.format("%.2f", (double) (values.get(i) - values.get(i - 1)) / (double) values.get(i - 1) * 100));
            } else {
                values2.add("0");
            }
        }
        try {
            //判断是区间内
            if (!StringUtils.isEmpty(beginTime) && !StringUtils.isEmpty(endTime)) {
                values = values.subList(begin, end + 1);
                values2 = values2.subList(begin, end + 1);
            }
        } catch (Exception e) {
            return ResultBean.error("么有内容");
        }
        /**
         * 返回表格数据(后加)
         */
        List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        for (int i = 0; i < timess.size(); i++) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("time", timess.get(i));
            jsonObject1.put("orderCount", values.get(i));
            jsonObject1.put("scale", values2.get(i));
            jsonObjects.add(jsonObject1);
        }
        /**
         * 设置返回对象
         */
        ResultBean resultBean = new ResultBean();
        resultBean.put("keys", timess);
        resultBean.put("values", values);
        resultBean.put("values2", values2);
        resultBean.put("table", jsonObjects);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取订单区间商品数量分析
     *
     * @param data
     * @return
     */
    @PostMapping("/getOrderGoodsCount")
    public ResultBean getOrderGoodsCount(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取订单区间
         */
        List<String> analysis = countryService.getOrderAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,订单区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 条形图
         */
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getOrderGoodsCount(times, an, countryName));
            listList.add(jsonObject1);
        }
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }

    /**
     * 获取订单区间商品数量比例
     *
     * @param data
     * @return
     */
    @PostMapping("/getOrderGoodsCountScale")
    public ResultBean getOrderGoodsCountScale(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取订单区间
         */
        List<String> analysis = countryService.getOrderAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,订单区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 堆叠图
         */
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getOrderGoodsCountScale(times, an, countryName));
            listList.add(jsonObject1);
        }
        /**
         * 返回表格信息
         */
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取价格区间商品数量分析
     *
     * @param data
     * @return
     */
    @PostMapping("/getPriceGoodsCount")
    public ResultBean getPriceGoodsCount(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取价格区间
         */
        List<String> analysis = countryService.getPriceAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,价格区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        int begin = times.indexOf(beginTime);
        int end = times.indexOf(endTime) + 1;
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 条形图
         */
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getPriceGoodsCount(times, an, countryName));
            listList.add(jsonObject1);
        }
        /**
         * 返回表格信息
         */
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取订单区间商品数量比例
     *
     * @param data
     * @return
     */
    @PostMapping("/getPriceGoodsCountScale")
    public ResultBean getPriceGoodsCountScale(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取订单区间
         */
        List<String> analysis = countryService.getPriceAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,价格区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 堆叠图
         */
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getPriceGoodsCountScale(times, an, countryName));
            listList.add(jsonObject1);
        }
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }

    /**
     * 获取价格区间订单数量分析
     *
     * @param data
     * @return
     */
    @PostMapping("/getPriceOrderCount")
    public ResultBean getPriceOrderCount(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取价格区间
         */
        List<String> analysis = countryService.getPriceAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,价格区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 条形图
         */
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getPriceOrderCount(times, an, countryName));
            listList.add(jsonObject1);
        }
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取价格区间订单数量比例
     *
     * @param data
     * @return
     */
    @PostMapping("/getPriceOrderCountScale")
    public ResultBean getPriceOrderCountScale(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取订单区间
         */
        List<String> analysis = countryService.getPriceAnalysis(countryName);
        //四个区间
        if (analysis.size() != 4) {
            logger.error("查询" + countryName + "国家时数据错误,价格区间有误");
            return ResultBean.error("查询数据有误");
        }
        /**
         * 获取所有时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 堆叠图
         */
        List<JSONObject> listList = new ArrayList<>();
        for (String an : analysis) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", an);
            jsonObject1.put("data", countryService.getPriceGoodsCountScale(times, an, countryName));
            listList.add(jsonObject1);
        }
        ResultBean resultBean = getTable(analysis, listList, timess);
        resultBean.put("keys", timess);
        resultBean.put("values", analysis);
        resultBean.put("values2", listList);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取国家某个月份一级类目成交额   (国家分析 第七页 7-1)
     *
     * @param data
     * @return
     */
    @PostMapping("/getMainTitle")
    public ResultBean getMainTitle(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String time = jsonObject.getString("time");
        String countryName = jsonObject.getString("countryName");
        /**
         * 获取一级类目成交额
         */
        try {
            return ResultBean.success("获取数据成功", countryService.getMainTitle(countryName, time));
        } catch (Exception e) {
            return ResultBean.error("获取数据失败");
        }
    }


    /**
     * 获取某个国家一级类目的环比增幅   (国家分析 第七页 7-2)
     *
     * @param data
     * @return
     */
    @PostMapping("/getMainTitleAdd")
    public ResultBean getMainTitleAdd(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取所有月份
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        List<String> times2 = countryService.getAllTimeAsc2();
        int begin = times2.indexOf(beginTime);
        int end = times2.indexOf(endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time : times) {
            StringBuilder sb = new StringBuilder(time);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 获取所有一级类目
         */
        List<String> mainTitles = countryService.getAllMainTitle();
        List<JSONObject> lists = new ArrayList<JSONObject>();
        for (String mainTitle : mainTitles) {
            JSONObject jsonObject1 = new JSONObject();
            //存储环比增幅
            List<MainTitleScale> mainTitleScales = countryService.getMainTitleTurnVolume(countryName, times2, mainTitle);
            List<String> values = new ArrayList<String>();
            for (int i = 0; i < mainTitleScales.size(); i++) {
                if (i != 0) {
                    values.add(String.format("%.2f", (double) (mainTitleScales.get(i).getTurnVolume() - mainTitleScales.get(i - 1).getTurnVolume()) / (double) mainTitleScales.get(i - 1).getTurnVolume() * 100));
                } else {
                    values.add("0");
                }
            }
            jsonObject1.put("name", mainTitle);
            if (!StringUtils.isEmpty(beginTime) && !StringUtils.isEmpty(endTime)) {
                values = values.subList(begin, end + 1);
            }
            jsonObject1.put("data", values);
            lists.add(jsonObject1);
        }
        /**
         * 返回表格信息
         */
        ResultBean resultBean = getTable(mainTitles, lists, timess);
        resultBean.put("times", timess);
        resultBean.put("mainTitle", mainTitles);
        resultBean.put("values", lists);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取国家某个月份物流分析
     *
     * @param data
     * @return
     */
    @PostMapping("/getLogisticsOrderCount")
    public ResultBean getLogisticsOrderCount(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String time = jsonObject.getString("time");
        String countryName = jsonObject.getString("countryName");
        //获取物流信息
        List<LogisticsOrder> logisticsOrders = countryService.getLogistics(time, countryName);
        return ResultBean.success(logisticsOrders);
    }


    /**
     * 获取国家月份top5物流信息
     *
     * @param data
     * @return
     */
    @PostMapping("/getLogisticsLimit")
    public ResultBean getLogisticsLimit(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String countryName = jsonObject.getString("countryName");
        String beginTime = jsonObject.getString("beginTime");
        String endTime = jsonObject.getString("endTime");
        /**
         * 获取时间
         */
        List<String> times = countryService.getAllTimeAsc(beginTime, endTime);
        /**
         * 格式化日期
         */
        List<String> timess = new ArrayList<>();
        for (String time1 : times) {
            StringBuilder sb = new StringBuilder(time1);
            sb.insert(4, "年");
            sb.append("月");
            timess.add(sb.toString());
        }
        /**
         * 获取当月前五物流公司名称
         */
        List<String> logistics = countryService.getLogisticsNameByCount(endTime, countryName);
        List<JSONObject> listList = new ArrayList<JSONObject>();
        for (String logistic : logistics) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", logistic);
            jsonObject1.put("data", countryService.getLogisticsLimit(times, countryName, logistic));
            listList.add(jsonObject1);
        }
        ResultBean resultBean = getTable(logistics, listList, timess);
        resultBean.put("times", timess);
        resultBean.put("logisticsNames", logistics);
        resultBean.put("values", listList);
        resultBean.put("code", 200);
        return resultBean;
    }


    /**
     * 获取top10店铺
     *
     * @param data
     * @return
     */
    @PostMapping("/getShopTopTen")
    public ResultBean getShopTopTen(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String time = jsonObject.getString("time");
        String countryName = jsonObject.getString("countryName");
        return ResultBean.success(countryService.getShopTen(time, countryName));
    }


    /**
     * 获取top10商品
     *
     * @param data
     * @return
     */
    @PostMapping("/getGoodsTopTen")
    public ResultBean getGoodsTopTen(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        String time = jsonObject.getString("time");
        String countryName = jsonObject.getString("countryName");
        return ResultBean.success(countryService.getGoodsTen(time, countryName));
    }

    /**
     * 获取top1店铺价格带销售额分布
     *
     * @param data
     * @return
     */
    @PostMapping("/getTopOneShopCase")
    public ResultBean getTopOneShopCase(@RequestBody String data) {
        try{
            /**
             * 解析参数
             */
            JSONObject jsonObject = JSONObject.parseObject(data);
            String time = jsonObject.getString("time");
            String countryName = jsonObject.getString("countryName");
            //更改内容，表格没删除暂时待定
            //return ResultBean.success(countryService.getTopOneShopCase(time, countryName));
            String array = jsonObject.getString("array");
            //存储返回内容
            List<JSONObject> objectList = new ArrayList<JSONObject>();
            //查询内容
            String shopName = countryService.getTopOneShop(time,countryName);
            List<CountryToponeShopMsg> countryToponeShopMsgList = countryService.getShops(shopName);
            if(StringUtils.isEmpty(array) || array == null){
                ResultBean resultBean = getTable(countryToponeShopMsgList,time,countryName);
                resultBean.put("shopName",shopName);
                return resultBean;
            }else{
                ResultBean resultBean = getTable(countryToponeShopMsgList,array,time,countryName);
                resultBean.put("shopName",shopName);
                return resultBean;
            }
        }catch (Exception e){
            return ResultBean.error("查询数据失败");
        }
    }


    /**
     * 返回表格信息  (将图表数据给处理成一个表格形式，供前端使用)
     *
     * @param heads  各个名称或区间等集合
     * @param data   整理的某个名称或区间下的各个时间的内容集合
     * @param timess 时间集合
     * @return
     */
    public ResultBean getTable(List<String> heads, List<JSONObject> data, List<String> timess) {
        //处理表头数据x
        List<Object> tableHead = new ArrayList<Object>();
        //将四个区间添加到表头中
        for (int i = 0; i < heads.size(); i++) {
            JSONObject head = new JSONObject();
            head.put("name", heads.get(i));
            head.put("filedName", "field" + (i + 1));
            tableHead.add(head);
        }
        //将时间列放在第一列中
        JSONObject firstHead = new JSONObject();
        firstHead.put("name", "日期");
        firstHead.put("filedName", "field" + 0);
        tableHead.add(0, firstHead);
        //处理表格每一行内容
        List<Object> tableContent = new ArrayList<Object>();
        //循环每个时间
        for (int i = 0; i < timess.size(); i++) {
            JSONObject line = new JSONObject();
            line.put("field0", timess.get(i));
            //取到时间后，去取每个时间对应的四个区间内容
            for (int j = 1; j < tableHead.size(); j++) {
                line.put("field" + j, data.get(j - 1).getJSONArray("data").get(i));
            }
            tableContent.add(line);
        }
        ResultBean resultBean = new ResultBean();
        resultBean.put("tableHead", tableHead);
        resultBean.put("tableContent", tableContent);
        return resultBean;
    }


    /**
     * 整理第一店铺信息  没有传入区间
     * @param countryToponeShopMsgList
     * @return
     */
    public ResultBean getTable(List<CountryToponeShopMsg> countryToponeShopMsgList,String time,String countryName){
        try{
            List<JSONObject> objectList = new ArrayList<>();
            //整理数据
            //计算价格区间步长
            double minPrice = countryToponeShopMsgList.get(0).getPrice();
            double maxPrice = countryToponeShopMsgList.get(countryToponeShopMsgList.size()-1).getPrice();
            double stepSize = (double) Math.round((maxPrice-minPrice) * 100/5) / 100;
            //计算区间统计商品个数
            double j = minPrice;
            for(double i=minPrice+stepSize;i<maxPrice+stepSize;i+=stepSize){
                if(objectList.size() == 4){
                    i = maxPrice;
                }
                JSONObject jsonObject1 = new JSONObject();
                int count = 0;
                double totalPrice = 0d;
                //最后区间的数字跟最大数据相同
                if(i > maxPrice){
                    jsonObject1.put("priceSection",String.format("%.2f",j) + "~" + String.format("%.2f",maxPrice));
                }else{
                    jsonObject1.put("priceSection",String.format("%.2f",j) + "~" + String.format("%.2f",i));
                }
                for(int m = 0;m<countryToponeShopMsgList.size(); m++){
                    double price = countryToponeShopMsgList.get(m).getPrice();
                    //因为升序排列，则可以直接跳出循环，方便遍历
                    if(price > i){
                        break;
                    }
                    if(j <= price && price <= i){
                        count ++;
                        totalPrice += countryToponeShopMsgList.get(m).getTotalSales();
                        countryToponeShopMsgList.remove(countryToponeShopMsgList.get(m));
                        m--;
                    }
                }
                jsonObject1.put("goodsCount",count);
                jsonObject1.put("createTime",time);
                jsonObject1.put("countryName",countryName);
                jsonObject1.put("saleRoom",String.format("%.2f",totalPrice));
                objectList.add(jsonObject1);
                j = i;
            }
            ResultBean resultBean = new ResultBean();
            resultBean.put("maxPrice",maxPrice);
            resultBean.put("minPrice",minPrice);
            resultBean.put("data",objectList);
            resultBean.put("code",200);
            return resultBean;
        }catch (Exception e){
            return ResultBean.error("查询数据失败");
        }
    }

    /**
     * 整理第一店铺信息  传入区间
     * @param countryToponeShopMsgList
     * @return
     */
    public ResultBean getTable(List<CountryToponeShopMsg> countryToponeShopMsgList,String data,String time,String countryName){
        try{
            String[] arrays = data.substring(1,data.length()-1).split(",");
            List<JSONObject> objectList = new ArrayList<>();
            double minPrice = countryToponeShopMsgList.get(0).getPrice();
            double maxPrice = countryToponeShopMsgList.get(countryToponeShopMsgList.size()-1).getPrice();
            for(int i=0;i<arrays.length;i++){
                JSONObject jsonObject1 = new JSONObject();
                if(arrays[i].equals(arrays[arrays.length-1])){
                    break;
                }
                jsonObject1.put("priceSection",arrays[i] + "~" + arrays[i+1]);
                int count = 0;
                double totalPrice = 0d;
                for(int m = 0;m<countryToponeShopMsgList.size(); m++){
                    double price = countryToponeShopMsgList.get(m).getPrice();
                    //因为升序排列，则可以直接跳出循环，方便遍历
                    if(price > Double.valueOf(arrays[i+1])){
                        break;
                    }
                    if(Double.valueOf(arrays[i]) <= price && price <= Double.valueOf(arrays[i+1])){
                        count ++;
                        totalPrice += countryToponeShopMsgList.get(m).getTotalSales();
                        countryToponeShopMsgList.remove(countryToponeShopMsgList.get(m));
                        m--;
                    }
                }
                jsonObject1.put("goodsCount",count);
                jsonObject1.put("createTime",time);
                jsonObject1.put("countryName",countryName);
                jsonObject1.put("saleRoom",String.format("%.2f",totalPrice));
                objectList.add(jsonObject1);
            }
            ResultBean resultBean = new ResultBean();
            resultBean.put("maxPrice",maxPrice);
            resultBean.put("minPrice",minPrice);
            resultBean.put("data",objectList);
            resultBean.put("code",200);
            return resultBean;
        }catch (Exception e){
            return getTable(countryToponeShopMsgList,time,countryName);
        }
    }
}